﻿using System.Web.Routing;
using ActivateYourGlutes.Controllers;

namespace System.Web.Mvc
{
    public static class ControllerHelperExtensions
    {
        private const string UnauthorisedAccessExceptionMessage =
            "Unauthorised access attempt by user {0}";
        public static RedirectToRouteResult RedirectToUnauthorized(this LocalisingController controller)
        {
            if (controller.Logger != null)
            {
                if (controller.User != null)
                {
                    controller.Logger.Warning(String.Format(UnauthorisedAccessExceptionMessage,
                                                            controller.User.Identity.Name));
                }
                else
                {
                    controller.Logger.Warning(String.Format(UnauthorisedAccessExceptionMessage, "unknown"));
                }
            }
            
            return new RedirectToRouteResult(new RouteValueDictionary(new { controller="Account", action="Unauthorized" }));
        }
    }
}
